package com.neuedu.api;

import java.io.UnsupportedEncodingException;

/**
 * 学习String
 */
public class MyString {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String str1 = "hello string";
        String str2 = new String("hello string");

        char chars1[] = {'h','e','l','l','o'};
        String str3 = new String(chars1);

        char chars2[] = {'h','e','l','l','o' ,' ' ,'w','o','r','l','d'};
        String str4= new String(chars2,6,chars2.length-6);

        System.out.println("str3 = " + str3);
        System.out.println("str4 = " + str4);


        //对象的比较
        System.out.println("(str1 == str2) = " + (str1 == str2));
        System.out.println("(str1 .equals(str2))= " + (str1 .equals(str2)));



        //字符串的 方法
        String info1 = "Hello world";
        System.out.println("info1 = " + info1);
        System.out.println("info1.length() = " + info1.length());
        System.out.println("info1.indexOf('o') = " + info1.indexOf('o'));

//        System.out.println("info1.charAt(0) = " + info1.charAt(0));
//        System.out.println("info1.charAt(1) = " + info1.charAt(1));

        System.out.println("\r\n\r\n字符串遍历");
        char chare = 'e';
        System.out.println("(int)'e' = " + (int) 'e');
        //遍历每一个字符
        for (int i = 0; i < info1.length(); i++) {
            System.out.println("info1.charAt("+i+") = " + info1.charAt(i) +"\tcodePointAt（"+i+"）:"+info1.codePointAt(i));
        }

        //字符串的拼接
        String concat1 = "helllo";
        String concat2 = "world";
        //产生了新对象
        String concatStr  = concat1.concat(concat2);

        System.out.println("concat1 = " + concat1);
        System.out.println("concatStr = " + concatStr);


        /**
         * 将String对象中的所有字符都转换为大写
         * String toLowerCase( )	将String对象中的所有字符都转换为小写
         */

        String info = "Hello World";
        System.out.println("info = " + info);
        System.out.println("info.toUpperCase() = " + info.toUpperCase());
        System.out.println("info.toLowerCase() = " + info.toLowerCase());
        System.out.println("info " + info);

        //查找
        System.out.println("info.indexOf('l') = " + info.indexOf('l'));
        System.out.println("info.indexOf('l'，4) = " + info.indexOf('l',4));
        System.out.println("info.lastIndexOf('l') = " + info.lastIndexOf('l'));

        //截取
        System.out.println("info.substring(6) =" + info.substring(6));
        System.out.println("info.substring(6,8) =" + info.substring(6,8));
        System.out.println("info " + info);

        //去除前后空
        System.out.println("\"     Hello     \".length(): "+"   Hello  ".length());
        System.out.println("\"   Hello  \".trim().length() "+"   Hello  ".trim().length());



        //字符串地退换
        String introduce  = "My Name is jshand";
        System.out.println("introduce = " + introduce);
        System.out.println("introduce.replace(\"is\",\"are\") = " + introduce.replace("is", "are"));
        System.out.println("introduce = " + introduce);


        //字符串拆分
        String splits[] = introduce.split(" ");
        System.out.println("splits = " + splits);

        byte[] bytes1 = introduce.getBytes();
        byte[] gbks = introduce.getBytes("GBK");
        byte[] utf8s = introduce.getBytes("utf-8");


        System.out.println("  End  Program ");


        String oldString = "hello world";
        oldString = oldString.substring(0,5);
        System.out.println("oldString = " + oldString);
    }


}
